package lijinyuan.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lijinyuan.pojo.Dept;
import lijinyuan.pojo.LJYPojo;
import lijinyuan.pojo.Page;
import lijinyuan.pojo.PageUtils;
import lijinyuan.service.LJYService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

//
@Controller
public class LJYController {
   @Resource
   LJYService ljyservice;
   LJYPojo ljy;

    @RequestMapping("showAll")
    @ResponseBody
    public String showAll(String name, Integer pageNo){
        ljy=new LJYPojo();
        ljy.setName(name);

        int totalCount=ljyservice.queryOne(ljy).size();
        Page page= PageUtils.getPage(5,totalCount,pageNo);
        List<LJYPojo> list= ljyservice.queryPage(page,ljy);
        page.setData(list);
        //return JSONArray.toJSONString(list);
        return JSONObject.toJSONString(page);
    }

    @RequestMapping("addOne")
    @ResponseBody
    public String addOne(LJYPojo pojo){
        if(pojo.getId()!=null && pojo.getId()>0){
            ljyservice.updateOne(pojo);//修改方法
        }else{
            ljyservice.addOne(pojo);//添加方法
        }

        return "null";
    }

    @RequestMapping("showOneId")
    @ResponseBody
    public String showOneId(Integer id){
        ljy=new LJYPojo();
        ljy.setId(id);
        ljy=ljyservice.showOneId(ljy);
        return JSONObject.toJSONString(ljy);
    }

    @RequestMapping("delOneId")
    @ResponseBody
    public String delOneId(LJYPojo pojo){
        ljyservice.delOneId(pojo);
        return "null";
    }

    @RequestMapping("dept_showAll")
    @ResponseBody
    public String dept_showAll(){
        List<Dept> list =ljyservice.dept_showAll();
        return JSONArray.toJSONString(list);
    }

    @RequestMapping("login")
    @ResponseBody
    public String login(String name,String password){
        String flag=ljyservice.login(name,password);
        return JSONObject.toJSONString(flag);
    }
}
